using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using Entity;

namespace AutoGear.BLL
{
    public class News
    {
        #region News Private Fields

        int _newsId;
        string _newsName = string.Empty;
        string _newsBriefDescription = string.Empty;
        string _newsDescription = string.Empty;
        string _newsImageUrl = string.Empty;
        string _displayOrder = string.Empty;
        DateTime _newsDate;
        bool _isActive;
        bool _isDeleted;
        DateTime _createDate;
        DateTime _updateDate;
        ReadWriteCustomCollection<News> _relatedNews;
        #endregion

        #region News Constractor
        public News() { }

        public News
        (
            int newsId,
            string newsName,
            string newsBriefDescription,
            string newsDescription,
            string newsImageUrl,
            DateTime newsDate,
            string displayOrder,            
            bool isActive,
            bool isDeleted,
            DateTime createDate,
            DateTime updateDate
        )
        {
            this._newsId = newsId;
            this._newsName = newsName;
            this._newsBriefDescription = newsBriefDescription;
            this._newsDescription = newsDescription;
            this._newsImageUrl = newsImageUrl;
            this._displayOrder = displayOrder;
            this._newsDate = newsDate;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
            this._createDate = createDate;
            this._updateDate = updateDate;
        }
        public News
       (
           string newsName,
           string newsBriefDescription,
           string newsDescription,
           string newsImageUrl,
           DateTime newsDate,
           string displayOrder,
           bool isActive,
           bool isDeleted
       )
        {
            this._newsName = newsName;
            this._newsBriefDescription = newsBriefDescription;
            this._newsDescription = newsDescription;
            this._newsImageUrl = newsImageUrl;
            this._newsDate = newsDate;
            this._displayOrder = displayOrder;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
        }
        #endregion

        #region Public Properties
        public int NewsId
        {
            get { return _newsId; }
        }
        public string NewsName
        {
            get { return _newsName; }
            set { _newsName = value; }
        }
        public string NewsBriefDescription
        {
            get { return _newsBriefDescription; }
            set { _newsBriefDescription = value; }
        }
        public string NewsDescription
        {
            get { return _newsDescription; }
            set { _newsDescription = value; }
        }
        public string NewsImageUrl
        {
            get { return _newsImageUrl; }
            set { _newsImageUrl = value; }
        }
         public DateTime NewsDate
        {
            get { return this._newsDate; }
            set { this._newsDate = value; }
        }
        public string DisplayOrder
        {
            get { return _displayOrder; }
            set { _displayOrder = value; }
        }
        public bool IsActive
        {
            get { return this._isActive; }
            set { this._isActive = value; }
        }
        public bool IsDeleted
        {
            get { return this._isDeleted; }
            set { this._isDeleted = value; }
        }
        public DateTime CreateDate
        {
            get { return this._createDate; }
        }
        public DateTime UpdateDate
        {
            get { return this._updateDate; }
            set { this._updateDate = value; }
        }

        public ReadWriteCustomCollection<News> RelatedNews
        {
            get
            {
                _relatedNews = new ReadWriteCustomCollection<News>();
                ECommerce.DAL.CustomCollection<TblNewsRelated> related = TblNewsRelated.GetTblNewsRelatedByNewsId(this._newsId);
                if (related != null)
                {
                    for (int i = 0; i < related.Count; i++)
                    {
                        try
                        {
                            News news = News.Load(related[i].RelatedNewsId);
                            if (news != null && news.IsActive)
                            {
                                _relatedNews.Add(news);
                            }
                        }
                        catch { }
                    }
                }
                return this._relatedNews;
            }
        }
        #endregion
        
        public void Create()
        {

            try
            {
                this._newsId = TblNews.CreateNewTblNews(this._newsName, this._newsBriefDescription, this._newsDescription, this._newsImageUrl,this._newsDate, this._displayOrder, this._isActive, this._isDeleted, DateTime.Now, DateTime.Now);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Update()
        {
            try
            {
                TblNews tblNews = TblNews.GetTblNewsByNewsId(this._newsId);
                tblNews.NewsName = this._newsName;
                tblNews.NewsBriefDescription = this._newsBriefDescription;
                tblNews.NewsDescription = this._newsDescription;
                tblNews.NewsImageUrl = this._newsImageUrl;
                tblNews.NewsDate = this._newsDate;
                tblNews.DisplayOrder = this._displayOrder;
                tblNews.IsActive = this._isActive;
                tblNews.IsDeleted = this._isDeleted;
                tblNews.UpdateDate = DateTime.Now;
                return tblNews.Save();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Delete()
        {
            /**************************************************
             * will update database table, will delete physical
             * files and will write in admin log about the 
             * delete
             **************************************************/
            this._isDeleted = true;
            this.Update();
        }

        public static void DeleteNews(ReadWriteCustomCollection<News> News)
        {
            foreach (News news in News)
            {
                news.Delete();
            }
        }

        public void AddRelatedNews(News news)
        {
            if (news.NewsId == this.NewsId)
                return;
            ECommerce.DAL.CustomCollection<TblNewsRelated> relateds = TblNewsRelated.GetTblNewsRelatedByNewsId(this._newsId);
            if (relateds != null)
            {
                for (int i = 0; i < relateds.Count; i++)
                {
                    if (news.NewsId == relateds[i].RelatedNewsId)
                    {
                        return;
                    }
                }
            }
            this.RelatedNews.Add(news);
            TblNewsRelated related = new TblNewsRelated();
            TblNewsRelated.CreateNewTblNewsRelated(this._newsId, news._newsId);
        }

        public void DeleteRelatedNews(int newsId)
        {
            ECommerce.DAL.CustomCollection<TblNewsRelated> related = TblNewsRelated.GetTblNewsRelatedByNewsId(this._newsId);
            if (related != null)
            {
                for (int i = 0; i < related.Count; i++)
                {
                    if (related[i].RelatedNewsId == newsId)
                    {
                        TblNewsRelated.DeleteTblNewsRelated(related[i].TblNewsRelatedId);
                        break;
                    }
                }
            }
        }

        public static ReadWriteCustomCollection<News> GetNews(int howMany)
        {
            try
            {
                ECommerce.DAL.CustomCollection<TblNews> tblNews = TblNewsExtended.GetActiveNewsByCount(howMany);
                ReadWriteCustomCollection<News> News = LoadFromCustomCollection(tblNews);
                return News;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static News Load(int newsId)
        {
            try
            {
                TblNews tmpTblNews = TblNews.GetTblNewsByNewsId(newsId);
                News news = new News(tmpTblNews.NewsId, tmpTblNews.NewsName, tmpTblNews.NewsBriefDescription, tmpTblNews.NewsDescription, tmpTblNews.NewsImageUrl, tmpTblNews.NewsDate, tmpTblNews.DisplayOrder, tmpTblNews.IsActive, tmpTblNews.IsDeleted, tmpTblNews.CreateDate, tmpTblNews.UpdateDate);
                return news;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static ReadWriteCustomCollection<News> GetBackendNewsListByNewsName(string NewsName, bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblNewsExtended.GetAllNewsByNewsNamePaged(NewsName, orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblNewsExtended.GetNonDeletedNewsByNewsNamePaged(NewsName, orderBy, startIndex, pageSize));
            }
        }

        public static ReadWriteCustomCollection<News> GetBackendNewsList(bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblNews.GetAllTblNewsPaged(orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblNewsExtended.GetNonDeletedNewsPaged(orderBy, startIndex, pageSize));
            }
        }

        public static int GetBackendNewsListCount(bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblNews.GetAllTblNewsPagedCount("", 0, 0);
            }
            else
            {
                return TblNewsExtended.GetNonDeletedNewsCount();
            }
        }

        public static int GetBackendNewsListByNewsNameCount(string NewsName, bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblNewsExtended.GetAllNewsByNewsNameCount(NewsName);
            }
            else
            {
                return TblNewsExtended.GetNonDeletedNewsByNewsNameCount(NewsName);
            }
        }

        private static ReadWriteCustomCollection<News> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblNews> tblNews)
        {
            ReadWriteCustomCollection<News> result = new ReadWriteCustomCollection<News>();
            for (int i = 0; i < tblNews.Count; i++)
            {
                TblNews tmpTblNews = tblNews[i];
                result.Add(new News(tmpTblNews.NewsId, tmpTblNews.NewsName, tmpTblNews.NewsBriefDescription, tmpTblNews.NewsDescription, tmpTblNews.NewsImageUrl, tmpTblNews.NewsDate, tmpTblNews.DisplayOrder, tmpTblNews.IsActive, tmpTblNews.IsDeleted, tmpTblNews.CreateDate, tmpTblNews.UpdateDate));
            }
            return result;
        }


    }
}
